﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using Premiere.Framework;
using Premiere.DTO;
using Gizmox.WebGUI.Forms;
using System.Web.UI;
using System.Data;
using Gizmox.WebGUI.Common.Interfaces;
using System.Drawing;

namespace PremiereMX
{
    public class Utils
    {
        public static IList GetListCode(IList dtos)
        {
            IList result = new ArrayList();
            foreach(CodedDTO cod in dtos)
            {
                result.Add(cod.Code);
            }
            return result;
        }
        public static IList GetListName(IList dtos)
        {
            IList result = new ArrayList();
            foreach (NamedNoteDTO cod in dtos)
            {
                result.Add(cod.Name);
            }
            return result;
        }
        public static IList GetListToString(IList dtos)
        {
            IList result = new ArrayList();
            foreach (NamedNoteDTO cod in dtos)
            {
                result.Add(cod.ToString());
            }
            return result;
        }
        public static bool Print(HtmlTextWriter objHtmlWriter, ListView list)
        {
            objHtmlWriter.WriteBeginTag("TABLE");
            objHtmlWriter.WriteAttribute("border", "1");
            objHtmlWriter.Write(">");
            objHtmlWriter.WriteFullBeginTag("TR");
            foreach (ColumnHeader objColumn in list.Columns)
            {
                objHtmlWriter.WriteFullBeginTag("TD");
                objHtmlWriter.Write(objColumn.Label);
                objHtmlWriter.WriteEndTag("TD");
            }
            objHtmlWriter.WriteEndTag("TR");

            foreach (ListViewItem objItem in list.Items)
            {
                objHtmlWriter.WriteFullBeginTag("TR");
                foreach (ListViewItem.ListViewSubItem objSubItem in objItem.SubItems)
                {
                    objHtmlWriter.WriteFullBeginTag("TD");
                    objHtmlWriter.Write(objSubItem.Text);
                    objHtmlWriter.WriteEndTag("TD");
                }
                objHtmlWriter.WriteEndTag("TR");
            }
            objHtmlWriter.WriteEndTag("TABLE");
            return true;
        }
        public static bool Print(HtmlTextWriter objHtmlWriter, PictureBox pb)
        {
            if (pb.Image != null)
            {
                objHtmlWriter.WriteBeginTag("IMG");
                objHtmlWriter.WriteAttribute("src", pb.Image.ToString());
                objHtmlWriter.Write("/>");
            }
            return true;
        }
        public static HtmlTextWriter PrintDataSet(IContext objContext, DataSet ds)
        {
            System.Web.UI.HtmlTextWriter objHtmlWriter = new System.Web.UI.HtmlTextWriter(objContext.HttpContext.Response.Output);
            objHtmlWriter.WriteBeginTag("HTML");
            objHtmlWriter.Write("><title>Print Preview</title>");
            objHtmlWriter.WriteBeginTag("BODY");
            objHtmlWriter.Write(" onload='window.print();window.close();' >");
            System.Web.UI.WebControls.DataGrid grid =
                  new System.Web.UI.WebControls.DataGrid();
            grid.HeaderStyle.Font.Bold = true;
            grid.HeaderStyle.Font.Name = "Calibri";
            grid.HeaderStyle.BackColor = Color.Gold;
            grid.Font.Name = "Calibri";
            grid.Font.Size = 9;
            grid.DataSource = ds;
            grid.DataMember = "Table";
            grid.DataBind();
            grid.RenderControl(objHtmlWriter);
            objHtmlWriter.WriteEndTag("BODY");
            objHtmlWriter.WriteEndTag("HTML");
            objHtmlWriter.Flush();
            return objHtmlWriter;
        }
        public static CodedDTO FindDTOInList(string code, IList list)
        {
            CodedDTO result = null;
            foreach (CodedDTO pc in list)
            {
                if (pc.Code == code)
                {
                    result = pc;
                    break;
                }
            }
            return result;
        }
        public static CodedDTO FindDTOInListToString(string tostring, IList list)
        {
            CodedDTO result = null;
            foreach (CodedDTO pc in list)
            {
                if (pc.ToString() == tostring)
                {
                    result = pc;
                    break;
                }
            }
            return result;
        }
    }
}
